package com.un.ebs.quality.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.un.ebs.core.exception.BusinessEntityNotFoundException;
import com.un.ebs.core.util.StringUtils;
import com.un.ebs.purchase.domain.PurchaseOrder;
import com.un.ebs.purchase.mapper.PurchaseOrderMapper;
import com.un.ebs.quality.dto.QualityDefineCheckListDto;
import com.un.ebs.quality.dto.QualityDefineUpdateDto;
import com.un.ebs.quality.enums.QualityTypeEnum;
import com.un.ebs.quality.mapper.QualityDefineMapper;
import com.un.ebs.quality.domain.QualityDefine;
import com.un.ebs.quality.service.QualityDefineService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * (QualityDefine)表服务实现类
 *
 * @author liubo
 * @since 2020-11-15 20:12:24
 */
@Service("qualityDefineService")
public class QualityDefineServiceImpl extends ServiceImpl<QualityDefineMapper, QualityDefine> implements QualityDefineService {


    @Override
    public void save(QualityDefineUpdateDto req) {
        QualityDefine obj;
        if (StringUtils.isNullOrEmpty(req.getId())) {
            obj = new QualityDefine();
        } else {
            obj = baseMapper.selectById(req.getId());
            if (obj == null) {
                throw new BusinessEntityNotFoundException(req.getId(), "质量定义");
            }
        }

        obj.setCode(req.getCode());
        obj.setTitle(req.getTitle());
        obj.setRemark(req.getRemark());
        obj.setQualityType(req.getQualityType());
        obj.insertOrUpdate();
    }

    @Override
    public void delete(String id) {
        baseMapper.deleteById(id);
    }

    @Override
    public QualityDefine queryById(String id) {
        return baseMapper.selectById(id);
    }

    @Override
    public IPage<QualityDefineCheckListDto> query(String searchText, QualityTypeEnum type, String productionWorkOrderId, Double planCheckQty, int pageSize, int pageIndex) {
        Page<QualityDefine> page = new Page<>(pageIndex, pageSize);
        LambdaQueryWrapper<QualityDefine> lq = Wrappers.lambdaQuery();
        if (type != null) {
            lq.eq(QualityDefine::getQualityType, type);
        }
        if (!StringUtils.isNullOrEmpty(searchText)) {
            lq.like(QualityDefine::getCode, searchText).like(QualityDefine::getTitle, searchText);
        }
        lq.orderByAsc(QualityDefine::getCode);
        IPage<QualityDefine> source = baseMapper.selectPage(page, lq);
        IPage<QualityDefineCheckListDto> ret = new Page<>(pageIndex, pageSize);
        BeanUtils.copyProperties(source, ret);

        ret.setCurrent(source.getCurrent());
        ret.setPages(source.getPages());
        ret.setSize(source.getSize());
        ret.setTotal(source.getTotal());

        List<QualityDefineCheckListDto> list= new ArrayList<>();
        for (QualityDefine item : ret.getRecords()) {
            QualityDefineCheckListDto obj = new QualityDefineCheckListDto();
            BeanUtils.copyProperties(item,obj);
            obj.setProductionWorkOrderId(productionWorkOrderId);
            obj.setPlanCheckQty(planCheckQty);
            list.add(obj);
        }
        ret.setRecords(list);
        return ret;
    }
}